package ltd.followkba.page;


import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.kcbsdk.AUserBase;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyManage.AUserFileType;

import java.util.List;

import ltd.advskin.MSKIN;
import ltd.advskin.base.BaseInput;
import ltd.advskin.base.BasePage;

import ltd.advskin.base.KcListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.base.MPAGE;
import ltd.advskin.task.TaskFree;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.task.App32;
import ltd.followkba.task.App33;
import ltd.followkba.task.BAppUser;
import ltd.followkba.task.BAppWorks;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.view.CloneView;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.mMyWorks;

public class OperationPage extends BasePage {
    private RadioButton rbAccount0;
    private RadioButton rbAccount1;
    private CloneView cvUserOperat;
    private View kbiCoverOperat;
    private View kbtWorksOperat;
    private View tvStateOperat;
    private View etIntroOperat;


    private BAppUser mBAppUser;
    private BAppWorks mBAppWorks;
    private byte gWorksType;
    private String[] mComment;

    private List<BDbsAccount> mBDbsAccount;
    private String[] mLoginFlag;
    private int gAppQty;
    private int gAppCount;
    private String gsLyric;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_operation);
        mKcHeaderView = findViewById(R.id.khvOperat);
        gWorksType = MAPP.mBDbsAccount.WorksType;
        String text = String.format("手动操作%s",  MAPP.mWorksName[gWorksType]);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });
        cvUserOperat = findViewById(R.id.cvUserOperat);
        cvUserOperat.tvCloneAccount.setVisibility(View.GONE);
        cvUserOperat.kbtSaveUser.setVisibility(View.GONE);

        kbiCoverOperat = findViewById(R.id.kbiCoverOperat);
        kbtWorksOperat = findViewById(R.id.kbtWorksOperat);
        tvStateOperat = findViewById(R.id.tvStateOperat);
        etIntroOperat = findViewById(R.id.etIntroOperat);
        rbAccount0 = findViewById(R.id.rbAccount0);
        rbAccount1 = findViewById(R.id.rbAccount1);

        if (gWorksType > 32) {
            findViewById(R.id.clRankOperat).setVisibility(View.GONE);
        }
        MSKIN.setText("请先选择用户", cvUserOperat.tvUserName);
        MSKIN.setText("选择用户", cvUserOperat.kbtSelectUser);
        cvUserOperat.setListener(new KcListener() {
            @Override
            public void onMessage(Object object) {
                switch ((int)object){
                    case 1:
                        MAPP.startUserHomePage(mBAppUser, false);       // 这个页面打开的是库存的WorksHomePage，不选择文件
                        break;
                    case 2:                                 // 保存克隆
                        break;
                    case 3:
                        MAPP.startSelectUserPage(true);
                        break;   // 其他用户
                }
            }
        });
        kbiCoverOperat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 作品封面
                MAPP.startUserHomePage(mBAppUser, true);
            }
        });


        CompoundButton.OnCheckedChangeListener onListener2 = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                MLOG("buttonView.getId() "+buttonView.getId());
                rbAccount0.setChecked(false);
                rbAccount1.setChecked(false);
                int select = 0;
                switch (buttonView.getId()) {
                    case R.id.rbAccount0: rbAccount0.setChecked(true); break;
                    case R.id.rbAccount1: rbAccount1.setChecked(true); select = 1; break;
                }
                MDBS.setFreeInt(gWorksType, "OperationAccount", select);
                setAccount(select);
                }
        };
        rbAccount0.setOnCheckedChangeListener(onListener2);
        rbAccount1.setOnCheckedChangeListener(onListener2);
        findViewById(R.id.kbtRankOperat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 从榜单原始列表之中找作品
                RankListPage rankListPage = new RankListPage();
                rankListPage.mBaseInput = new BaseInput();
                MPAGE.startBasePage(rankListPage);
            }
        });
        findViewById(R.id.kbtSelectOperat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 打开发送页面
                if (mBAppWorks != null) {
                    SendToPage sendToPage = new SendToPage();
                    boolean allAccount = MDBS.getFreeInt(gWorksType, "OperationAccount") > 0;
                    sendToPage.mBaseInput = new BaseInput(mBAppWorks, allAccount);
                    MPAGE.startBasePage(sendToPage);
                }else {
                    MTOAST("还没有选择作品");
                }
            }
        });
    }


    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据

            mBDbsAccount = MDBS.getBDbsAccount(gWorksType);

        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
                        showInfo();
                    }
                }
            }
        });
        loadInfo();
        getChecked();
        String text = MDBS.getFreeText(gWorksType, "OperationComment");
        showInfo();
    }
    @Override
    public void onPageUpdate(BasePage basePage){
        Object object = getPageResult(basePage);
        if (object instanceof BAppUser) {
            mBAppUser = (BAppUser)object;
            MDBS.setBAppUser(mBAppUser);
            MDBS.setFreeText(gWorksType, "OperationUser", mBAppUser.UserIdS);
            loadInfo();
        }
        if (object instanceof BAppWorks) {
            mBAppWorks = (BAppWorks)object;
            MDBS.setBAppWorks(mBAppWorks);
            MDBS.setFreeText(gWorksType, "OperationWorks", mBAppWorks.WorksIdS);
            loadInfo();
        }
//        onInitData(false);
    }
    private void showInfo() {
        if (mBDbsAccount != null){
            rbAccount1.setText(String.format("所有账号(%s)", mBDbsAccount.size()));
        }
        if (mBAppUser != null) {
            String appName =  MAPP.mWorksName[mBAppUser.WorksType];
            MSKIN.setText(appName, cvUserOperat.tvAppName);
            MSKIN.setText(String.format("ID:%s", mBAppUser.UserIdS), cvUserOperat.tvUserID);
            MSKIN.setText(mBAppUser.UserName, cvUserOperat.tvUserName);
            MAPP.showUserHead(mBAppUser.HeadFile, cvUserOperat.kbiHeadUser);
        }else {
            MSKIN.showUserHead(AUserFileType.IMG_SMALL, new AUserBase(), cvUserOperat.kbiHeadUser);
        }
        if (mBAppWorks != null){
            MAPP.showWorksCover(mBAppWorks, mBAppWorks.CoverFile, kbiCoverOperat);
            MAPP.showWorksName(mBAppWorks, kbtWorksOperat);
            MSKIN.setText(MAPP.getWorks(mBAppWorks), tvStateOperat);
            MSKIN.setText(mBAppWorks.Intro, etIntroOperat);
        }else {
            MSKIN.showWorksCover((String) null, kbiCoverOperat);
        }
    }

    private void loadInfo(){
        new TaskFree(null, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                if (isThread){
                    String userIdS = MDBS.getFreeText(gWorksType, "OperationUser");
                    String worksIdS = MDBS.getFreeText(gWorksType, "OperationWorks");
                    MLOG(String.format("RankA SelectB %s %s %s", gWorksType, userIdS, worksIdS));
                    if (userIdS != null) {
                        mBAppUser = MDBS.getBAppUser(gWorksType, userIdS);
                        if (worksIdS != null) {                          // 重新打开的
                            mBAppWorks = MDBS.getBAppWorks(gWorksType, userIdS, worksIdS);
                        }
                    }
                    return;
                }
                showInfo();
            }
        });
    }


    private void setAccount(int select){
        if (select == 0) {
            mLoginFlag = new String[1];
            mLoginFlag[0] = MAPP.mBDbsAccount.LoginFlag;
        } else if (mBDbsAccount != null) {
            int length = mBDbsAccount.size();
            mLoginFlag = new String[length];
            for (int index = 0; index < length; index++) {
                mLoginFlag[index] = mBDbsAccount.get(index).LoginFlag;
            }
        }
        gAppQty = mLoginFlag.length;
    }


    private void getChecked(){
        int[] tabViewId2 = {R.id.rbAccount0, R.id.rbAccount1};
        int select = MDBS.getFreeInt(gWorksType, "OperationAccount");
        for (int index = 0; index < tabViewId2.length; index++){
            RadioButton radioButton = ((RadioButton)findViewById(tabViewId2[index]));
            if (select == index){
                radioButton.setChecked(true);
            }else {
                radioButton.setChecked(false);
            }
        }
        setAccount(select);
    }


}
